package fhp.hlhj.giantfold.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.bumptech.glide.Glide
import de.hdodenhof.circleimageview.CircleImageView
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.javaBean.FriendsRankBean
import fhp.hlhj.giantfold.network.Urls
import java.text.DecimalFormat
import java.util.ArrayList

/**
 * author: FHP
 * data: on 2018\2\3 0003 10:30
 * E-Mail: 103945089@qq.com
 */
class FriendsRankAdp(private val context: Context,private val data:FriendsRankBean, var type:Int):BaseAdapter() {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var convertView=convertView
        if (convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.friend_rank_item,null)

        }
        val itemIcon = convertView?.findViewById<CircleImageView>(R.id.itemImg)
        val itemRank = convertView?.findViewById<TextView>(R.id.itemRank)
        val itemId = convertView?.findViewById<TextView>(R.id.itemId)
        val itemNum = convertView?.findViewById<TextView>(R.id.itemNum)

        when(type){
            1->{
                val rankingBean = data.global_ranking[position]
                val format = DecimalFormat("0.000")
                itemNum?.text= format.format(rankingBean.earnings_accumulated.toDouble()).toString()
                itemId?.text=rankingBean.nick_name
                Glide.with(context).load(Urls.BaseUrl+"/"+rankingBean.savepath+rankingBean.savename).asBitmap().placeholder(R.mipmap.placeholdericon).dontAnimate().into(itemIcon)
            }
            2->{
                val rankingBean = data.invited_users_ranking[position]
                val format = DecimalFormat("0.000")
                itemNum?.text= format.format(rankingBean.earnings_accumulated.toDouble()).toString()
                itemId?.text=rankingBean.nick_name

                Glide.with(context).load(Urls.BaseUrl+"/"+rankingBean.savepath+rankingBean.savename).asBitmap().placeholder(R.mipmap.placeholdericon).dontAnimate().into(itemIcon)
            }
        }
        when(position){
            0->{
                itemRank?.text=""
                itemRank?.setBackgroundResource(R.mipmap.fenxiagn_jinp)
            }
            1->{
                itemRank?.text=""
                itemRank?.setBackgroundResource(R.mipmap.fenxiagn_tp)

            }
            2->{
                itemRank?.text=""
                itemRank?.setBackgroundResource(R.mipmap.fenxiagn_ss)
            }
            else->{
                itemRank?.text=position.toString()
            }
        }
        return convertView
    }

    override fun getItem(position: Int): Any = when(type){
        1->{
            data.global_ranking[position]
        }
        2->{
            data.invited_users_ranking[position]
        }
        else->{
            0
        }
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return when(type){
            1->{
               data.global_ranking.size
            }
            2->{
                data.invited_users_ranking.size
            }
            else->{
                0
            }
        }
    }


    fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }
}